<?php
defined('IN_TS') or die('Access Denied.');

//发表评论 
if($ts=='addcomment'){
	if (IS_POST) {
		$talkid	= intval($_POST['talkid']);
		$content	= trim($_POST['content']);
		//标签
		doAction('add_comment','',$content,'');
		
		if($TS_USER['user'] == ''){
			qiMsg('请登陆后再发表内容^_^','点击登陆',SITE_URL.'index.php?app=user&ac=login');
		}elseif(empty($content)){
			qiMsg('没有任何内容是不允许你通过滴^_^');
		}else{
			$arrData	= array(
				'talkid'			=> $talkid,
				'userid'			=> $TS_USER['user']['userid'],
				'content'	=> $content,
				'addtime'		=> time(),
			);
			
			$commentid = $db->insertArr($arrData,dbprefix.'talk_topics_comments');
			
			//统计评论数
			$count_comment = $db->once_num_rows("select * from ".dbprefix."talk_topics_comments where talkid='$talkid'");
			
			//更新话题最后回应时间和评论数
			$uptime = time();
			
			$db->query("update ".dbprefix."talk_topics set uptime='$uptime',count_comment='$count_comment' where talkid='$talkid'");

			//发送系统消息(通知楼主有人回复他的话题啦)
			$strtalk = $db->once_fetch_assoc("select * from ".dbprefix."talk_topics where talkid='$talkid'");
			if($strtalk['userid'] != $TS_USER['user']['userid']){
			
				$msg_userid = '0';
				$msg_touserid = $strtalk['userid'];
				$msg_content = '你的话题：《'.$strtalk['title'].'》新增一条评论，快去看看给个回复吧^_^ <br />'
											.SITE_URL.'index.php?app=talk&ac=talk&talkid='.$talkid;
				aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content);				
			}
			$dangqianuser['user'] =aac('user')->getUserForApp($TS_USER['user']['userid']);
			$retime =date('Y-m-d H:i:s',time());
			$returnarr = array (
							'commentnum'   => $count_comment,
							'userid'       => $TS_USER['user']['userid'],
							'useravatar'   => $dangqianuser['user']['face'],
							'username'     => $dangqianuser['user']['username'],
							'createtime'   => $retime,
							'comment'      => $content,
							'id'		   => $commentid,
				);
				$jjj =json_encode($returnarr);
				echo $jjj;
		}	
	}
}

switch ($ts) {

	case "new_talk":
		$content	= trim($_POST['content']);
		$title	= htmlspecialchars(trim($_POST['title']));
		
		doAction('add_talk',$title,$content,'');		
		
		$typeid = intval($_POST['typeid']);
		
		$iscomment = $_POST['iscomment'];
		
		
		if($TS_USER['user'] == ''){
		
			qiMsg('请登陆后再发表内容^_^','点击登陆',SITE_URL.'index.php?app=user&ac=login');
			
		}elseif(empty($title)){

			$title = qiMsg('你忘了填写标题啦^_^');

		}elseif(empty($content)){

			qiMsg('没有任何内容是不允许你通过滴^_^');
			
		}elseif(mb_strlen($title,'utf8')>50){//限制发表内容多长度，默认为30
			
		 	qiMsg('标题很长很长很长很长...^_^');
		
		}elseif(mb_strlen($content,'utf8')>10000){//限制发表内容多长度，默认为1w
			
		 	qiMsg('发这么多内容干啥^_^');
		
		}else{
			
			$uptime = time();
			
			$arrData = array(
				'typeid'	          => $typeid,
				'userid'				=> $TS_USER['user']['userid'],
				'title'				=> $title,
				'content'		=> $content,
				'iscomment'		=> $iscomment,
				'addtime'			=> time(),
				'uptime'	=> $uptime,
			);
			
			$talkid = $db->insertArr($arrData,dbprefix.'talk_topics');
	
			
			header("Location: ".SITE_URL.tsurl('talk','talk',array(talkid=>$talkid)));
		}
		break;
		
		
		case "post_talk":
		$talkid	= intval($_POST['talkid']);
		$contentt	= trim($_POST['content']);
		$post = trim($_POST['post']);
		$price = trim($_POST['price']);
		$imgUrl = trim($_POST['imgUrl']);
		$title	= htmlspecialchars(trim($_POST['title']));
		$gourl	= trim($_POST['gourl']);
		
		$content = '<img src="'.$imgUrl.'" alt="'.$title.'"/><p style="text-align: center;font-size:14px;line-height:24px">'.$contentt.'</p>';
		//标签
		doAction('add_talk',$title,$content,'');		
		
		$typeid = intval($_POST['typeid']);
		
		$iscomment = $_POST['iscomment'];
		
		
		if($TS_USER['user'] == ''){
		
			qiMsg('请登陆后再发表内容^_^','点击登陆',SITE_URL.'index.php?app=user&ac=login');
			
		}elseif(empty($title)){

			$title = getsubstrutf8($content,0,60);
			
		}elseif(empty($gourl)){

			qiMsg('您提交的来源地址有误！');
			
		}elseif(empty($content)){

			qiMsg('没有任何内容是不允许你通过滴^_^');
			
		}elseif(mb_strlen($content,'utf8')>10000){//限制发表内容多长度，默认为1w
			
		 	qiMsg('发这么多内容干啥^_^');
		
		}else{
			
			$uptime = time();
			
			$arrData = array(
				'talkid'				=> $talkid,
				'typeid'	=> $typeid,
				'userid'				=> $TS_USER['user']['userid'],
				'title'				=> $title,
				'content'		=> $content,
				'price'		=> $price,
				'iscomment'		=> $iscomment,
				'addtime'			=> time(),
				'uptime'	=> $uptime,
			);
			
			//判断是否有图片和附件
			preg_match_all('/\[(photo)=(\d+)\]/is', $content, $isphoto);;
			if($isphoto[2] || strstr($content,'<img ')){
				$arrData['isphoto'] = '1';
			}
			//判断附件
			preg_match_all('/\[(attach)=(\d+)\]/is', $content, $isattach);
			if($isattach[2]){
				$arrData['isattach'] = '1';
			}
			
			$talkid = $db->insertArr($arrData,dbprefix.'talk_topics');
			
			//统计话题类型 
			if($typeid != '0'){
				$talkTypeNum = $db->once_num_rows("select * from ".dbprefix."talk_topics where typeid='$typeid'");
				$db->query("update ".dbprefix."talk_topics_type set `count_talk`='$talkTypeNum' where typeid='$typeid'");
			}
			//处理标签
			aac('tag')->addTag('talk','talkid',$talkid,$_POST['tag']);
			
			//统计秀店下话题数并更新
			$count_talk = $db->once_num_rows("select * from ".dbprefix."talk_topics where talkid='$talkid'");
			
			//统计今天发布话题数
			$today_start = strtotime(date('Y-m-d 00:00:00'));
			$today_end = strtotime(date('Y-m-d 23:59:59'));
			
			$count_talk_today = $db->once_num_rows("select * from ".dbprefix."talk_topics where talkid='$talkid' and addtime > '$today_start'");
			
			
			$db->query("update ".dbprefix."talk set count_talk='$count_talk',count_talk_today='$count_talk_today',uptime='$uptime' where talkid='$talkid'");
			
			
			
			//积分记录
			$userid = $TS_USER['user']['userid'];
			$db->query("insert into ".dbprefix."user_scores (`userid`,`scorename`,`score`,`addtime`) values ('".$userid."','分享','50','".time()."')");
			
			$strScore = $db->once_fetch_assoc("select sum(score) score from ".dbprefix."user_scores where userid='".$userid."'");
			
			//更新积分
			$db->query("update ".dbprefix."user_info set `count_score`='".$strScore['score']."' where userid='$userid'");
			
			//写入网址
			$db->query("insert into ".dbprefix."go_url (`talkid`,`gourl`) values ('".$talkid."','".$gourl."')");
			
			//重写内容加入图片
			
			if ($imgUrl){
		$db->query("UPDATE ".dbprefix."talk_topics SET content = '<a href=\"/go/url/goid-".$talkid."\" target=\"_blank\" title=\"查看购买信息\">".$content."</a>' WHERE talkid = $talkid");
		}
			
			//分享插件接口
			doAction('talk_add', $title,$talkid,$TS_USER,$TS_SITE,$TS_URL);
			
			header("Location: ".SITE_URL."index.php?app=talk&ac=talkover&talkid=".$talkid);
			
		}
		break;

	//删除评论回帖
	case "comment_del":
	
		$talkid = intval($_POST['talkid']);
		$commentid = intval($_POST['commentid']);
		
		$db->query("DELETE FROM ".dbprefix."talk_topics_comments WHERE commentid = '".$commentid."'");
		
		//统计
		$db->query("update ".dbprefix."talk_topics set count_comment=count_comment-1 where talkid='".$talkid."'");
		
		echo '0';
		
		
		break;
		
	
		
		
	//删除话题
	case "talk_del":
		$talkid = $_POST['talkid'];
		$userid = intval($TS_USER['user']['userid']);
		
		$strtalk = $db->once_fetch_assoc("select userid from ".dbprefix."talk_topics where talkid='$talkid'");
		
		
		if($userid == '1'){
			
			//删除话题
			$db->query("DELETE FROM ".dbprefix."talk_topics WHERE talkid = '".$talkid."'");
			//删除评论回复
			$db->query("DELETE FROM ".dbprefix."talk_topics_comments WHERE talkid = '".$talkid."'");
			
			echo '0';
			
		}else{

			echo '1';
		
		}
		
		break;
		
	//编辑话题 
	case "talk_eidt":
		
		$talkid = $_POST['talkid'];
		$title = htmlspecialchars(trim($_POST['title']));
		$typeid = $_POST['typeid'];
		$content = trim($_POST['content']);
		
		$iscomment = $_POST['iscomment'];
		
		$userid = intval($TS_USER['user']['userid']);
		
		if($talkid == '' || $title=='' || $content=='') qiMsg("都不能为空的哦!");
		
		$strtalk = $db->once_fetch_assoc("select * from ".dbprefix."talk_topics where talkid='".$talkid."'");

		if($TS_USER['user']['userid'] !== '1') qiMsg("机房重地，请勿进入！");
		
		$arrData = array(
			'typeid' => $typeid,
			'title' => $title,
			'content' => $content,
			'iscomment' => $iscomment,
		);
		
		//判断是否有图片和附件
		preg_match_all('/\[(photo)=(\d+)\]/is', $content, $isphoto);
		if($isphoto[2]){
			$arrData['isphoto'] = '1';
		}else{
			$arrData['isphoto'] = '0';
		}
		//判断附件
		preg_match_all('/\[(attach)=(\d+)\]/is', $content, $isattach);
		if($isattach[2]){
			$arrData['isattach'] = '1';
		}else{
			$arrData['isattach'] = '0';
		}
		
		$db->updateArr($arrData,dbprefix.'talk_topics','where talkid='.$talkid.'');

		header("Location: ".SITE_URL."index.php?app=talk&ac=talk&talkid=".$talkid);
		
		break;
		
	//隐藏显示话题
	case "talk_isshow":
	
		$talkid =intval($_POST['talkid']);
		$isshow = intval($_POST['isshow']);
		$isshow == 0 ? $isshow = 1 : $isshow = 0;
		$db->query("update ".dbprefix."talk_topics set isshow='$isshow' where talkid='$talkid'");
		echo '0';
		break;

	//回复评论
	case "recomment":
		$userid = $TS_USER['user']['userid'];
		$referid = $_POST['referid'];
		$talkid = $_POST['talkid'];
		$content = t($_POST['content']);
		$addtime = time();

		$db->query("insert into ".dbprefix."talk_topics_comments (`referid`,`talkid`,`userid`,`content`,`addtime`) values ('$referid','$talkid','$userid','$content','$addtime')");
		
		//统计评论数
		$count_comment = $db->once_num_rows("select * from ".dbprefix."talk_topics_comments where talkid='$talkid'");
		
		//更新话题最后回应时间和评论数
		$uptime = time();
		
		$db->query("update ".dbprefix."talk_topics set uptime='$uptime',count_comment='$count_comment' where talkid='$talkid'");
		
		$strtalk = $db->once_fetch_assoc("select * from ".dbprefix."talk_topics where talkid='$talkid'");
		$strComment = $db->once_fetch_assoc("select * from ".dbprefix."talk_topics_comments where commentid='$referid'");
		
		if($talkid && $strtalk['userid'] != $TS_USER['user']['userid']){
			$msg_userid = '0';
			$msg_touserid = $strtalk['userid'];
			$msg_content = '你的话题：《'.$strtalk['title'].'》新增一条评论，快去看看给个回复吧^_^ <br />'.SITE_URL.'index.php?app=talk&ac=talk&talkid='.$talkid;
			aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content);
		}
		
		if($referid && $strComment['userid'] != $TS_USER['user']['userid']){
			$msg_userid = '0';
			$msg_touserid = $strComment['userid'];
			$msg_content = '有人评论了你在话题：《'.$strtalk['title'].'》中的回复，快去看看给个回复吧^_^ <br />'.SITE_URL.'index.php?app=talk&ac=talk&talkid='.$talkid;
			aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content);
		}
		
		echo '0';
		
		break;
	
	//编辑话题类型
	case "edit_type":
		$typeid = $_POST['id'];
		$typename = t($_POST['value']);
		if(empty($typename)){
			echo '话题类型不能为空，请点击继续编辑';
		}else{
			$db->query("update ".dbprefix."talk_topics_type set `typename`='$typename' where typeid='$typeid'");
			echo $typename;
		}
		break;
	//删除话题类型 
	case "del_type":
		$typeid = $_POST['typeid'];
		$db->query("delete from ".dbprefix."talk_topics_type where typeid='$typeid'");
		$db->query("update ".dbprefix."talk_topics set typeid='0' where typeid='$typeid'");
		echo '0';
		break;
		
	//取消秀店分类
	case "cate_cancel":
		$cateid = intval($_POST['cateid']);
		$talkid = $_POST['talkid'];
		$db->query("delete from ".dbprefix."talk_cates_index where talkid='$talkid' and cateid='$cateid'");
		
		if($cateid > 0){
			
			//更新分类下秀店数
			$talknum = $db->once_num_rows("select * from ".dbprefix."talk_cates_index where cateid='$cateid'");
			
			$db->query("update ".dbprefix."talk_cates set `count_talk`='$talknum',`uptime`='$uptime' where cateid='$cateid'");
			
			
			//判断是否有顶级分类
			$strCate = $db->once_fetch_assoc("select catereferid from ".dbprefix."talk_cates where cateid='$cateid'");
			$catereferid = $strCate['catereferid'];
			
			if($catereferid > 0){
				//统计顶级分类下秀店数
				$talktotal = $db->once_fetch_assoc("select sum(`count_talk`) as `total` from ".dbprefix."talk_cates where catereferid='$catereferid'");
				
				$total = $talktotal['total'];
				
				$db->query("update ".dbprefix."talk_cates set `count_talk`='$total',`uptime`='$uptime' where cateid='$catereferid'");
			}
		}
		
		echo '0';
		break;
			
	//计算话题中是否有图片
	case "isphoto":
		$arrtalk = $db->fetch_all_assoc("select * from ".dbprefix."talk_topics");
		foreach($arrtalk as $item){
			$content = $item['content'];
			$talkid = $item['talkid'];
			preg_match_all('/\[(photo)=(\d+)\]/is', $content, $photo);
			if($photo[2]){
				$db->query("update ".dbprefix."talk_topics set `isphoto`='1' where talkid='$talkid'");
				echo '==OK==';
			}
		}
		break;
	case 'parseurl':
		function formPost($url,$post_data){
		  $o='';
		  foreach ($post_data as $k=>$v){
			  $o.= "$k=".urlencode($v)."&";
		  }
		  $post_data=substr($o,0,-1);
		  $ch = curl_init();
		  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
		  curl_setopt($ch, CURLOPT_POST, 1);
		  curl_setopt($ch, CURLOPT_HEADER, 0);
		  curl_setopt($ch, CURLOPT_URL,$url);
		  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		  $result = curl_exec($ch);
		  return $result;
		}

		$url = $_POST['parseurl'];
		$urlArr = parse_url($url);
		$domainArr = explode('.',$urlArr['host']);
		$data['type'] = $domainArr[count($domainArr)-2];
		$str = formPost('http://share.pengyou.com/index.php?mod=usershare&act=geturlinfo',array('url'=>$url));
		echo $str;
	
		break;
	
		
}